package com.cljz.peipei.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.cljz.peipei.R;
import com.cljz.peipei.base.App;
import com.cljz.peipei.base.BaseActivity;
import com.cljz.peipei.model.net.bean.LoginBean;
import com.cljz.peipei.model.net.bean.ResponseBean;
import com.cljz.peipei.present.activity.EditSmsCodeP;
import com.cljz.peipei.utils.ActivityManager;
import com.cljz.peipei.utils.T;
import com.cljz.peipei.widget.CodeEditText;
import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxTextView;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

/**
 * Created by apple on 2017/4/18.
 */

public class EditSmsCodeActivity extends BaseActivity<EditSmsCodeP> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.fl_back)
    FrameLayout flBack;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.cet_code)
    CodeEditText cetCode;
    @BindView(R.id.iv_logo)
    ImageView ivLogo;
    @BindView(R.id.tv_time)
    TextView tvTime;
    private String phone;
    private TimeCount timeCount;

    @Override
    public void initData(Bundle savedInstanceState) {
        phone = getIntent().getExtras().getString("phone");
        tvNumber.setText(phone);
        timeCount = new TimeCount(60000, 1000);
        timeCount.start();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_edit_sms;
    }

    @Override
    public EditSmsCodeP newP() {
        return new EditSmsCodeP();
    }

    @Override
    protected void initListener() {
        super.initListener();
        RxTextView.textChanges(cetCode)
                .compose(this.<CharSequence>bindToLifecycle())
                .subscribe(new Action1<CharSequence>() {
                    @Override
                    public void call(CharSequence charSequence) {
                        if (charSequence.length() == 6) {
                            showLoadingDialog();
                            getP().getLogin(phone, charSequence.toString().trim());
                        }
                    }
                });
        RxView.clicks(tvTime)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        getP().sendSmsCode(phone);
                    }
                });
        RxView.clicks(flBack)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.pop(EditSmsCodeActivity.this);
                    }
                });
    }

    public void loginFail(String error) {
        dismissLoadingDialog();
        T.getInstance().showShort(error);
        cetCode.setText("");
    }

    public void loginSucceed(LoginBean.DataBean data) {
        dismissLoadingDialog();
        if ("0".equals(data.getIs_new())) {
            Router.newIntent((Activity) App.getInstance())
                    .to(MainActivity.class)
                    .launch();
            ActivityManager.getInstance().finishAfterActivity(MainActivity.class);
        } else {
            Router.newIntent((Activity) App.getInstance())
                    .to(SetSexActivity.class)
                    .launch();
            ActivityManager.getInstance().finishAfterActivity(MainActivity.class);
        }
    }

    public void sendSmsSucceed(ResponseBean sendSmsCodeBean) {
        timeCount.start();
        tvTime.setTextColor(getResources().getColor(R.color.colorBtnBg));
        tvTime.setClickable(false);
        cetCode.setText("");
    }

    public void sendSmsFail(String error) {
        T.getInstance().showShort(error);
    }

    private class TimeCount extends CountDownTimer {

        TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long l) {
            tvTime.setVisibility(View.VISIBLE);
            tvTime.setText("接收大概需要" + l / 1000 + "s");
        }

        @Override
        public void onFinish() {
            tvTime.setText("重新发送验证码");
            tvTime.setTextColor(getResources().getColor(R.color.colorPrimary));
            tvTime.setClickable(true);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timeCount.cancel();
        timeCount = null;
    }
}
